<%@ page import="com.hdu.heihei.beans.User" %>
<%@ page import="com.hdu.heihei.service.impl.UserServiceImpl" %>
<%@ page import="com.hdu.heihei.service.impl.UserServiceImpl" %>
<%@ page import="com.hdu.heihei.beans.User" %>
<%--
  Created by IntelliJ IDEA.
  User: fengyuan
  Date: 2020/9/28
  Time: 22:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>login_check</title>
</head>
<body>
<%
    User user = new User();
    user.setUsername(request.getParameter("username"));
    user.setPassword(request.getParameter("password"));

    UserServiceImpl userServiceImpl = new UserServiceImpl();
    User loginedUser = userServiceImpl.login(user);
    if(loginedUser == null){
        out.print("<script language='javaScript'> alert('登录失败');</script>");
        response.setHeader("refresh", "0;url=../login.html");
    }else {
        Cookie cookieId = new Cookie("userId",String.valueOf(loginedUser.getId()));
        Cookie cookieUsername = new Cookie("username",loginedUser.getUsername());
        Cookie cookiePassword = new Cookie("password",loginedUser.getPassword());
        cookieId.setMaxAge(-1);
        cookieUsername.setMaxAge(-1);
        cookiePassword.setMaxAge(-1);
        cookieId.setPath(request.getContextPath());
        cookieUsername.setPath(request.getContextPath());
        cookiePassword.setPath(request.getContextPath());
        response.addCookie(cookieId);
        response.addCookie(cookieUsername);
        response.addCookie(cookiePassword);

        out.print("<script language='javaScript'> alert('登录成功');</script>");
        response.setHeader("refresh", "0;url=../views/edit.jsp");
    }
%>
</body>
</html>
